/**
 *
 */
package com.honeybees.framework.mybatis.sqlsource;

import java.util.List;

import org.apache.ibatis.builder.StaticSqlSource;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.ParameterMapping;
import org.apache.ibatis.mapping.SqlSource;
import org.apache.ibatis.reflection.MetaObject;
import org.apache.ibatis.reflection.SystemMetaObject;
import org.apache.ibatis.session.Configuration;

/**
 * <dl>
 * <dt><b> 默认的静态 SqlSource </b></dt>
 * <p>
 * <dd>功能描述</dd>
 * </dl>
 * <p>
 * Copyright (C) All rights reserved.
 * </p>
 *
 * @author 李远明
 * @since 2016年7月20日 下午8:29:02
 */
public class DefaultStaticSqlSource implements SqlSource {

    private String sql;
    private List<ParameterMapping> parameterMappings;
    private Configuration configuration;
    private SqlSource rawSqlSource;

    /**
     * 默认的静态 SqlSource
     *
     * @param sqlSource
     */
    @SuppressWarnings("unchecked")
    public DefaultStaticSqlSource(StaticSqlSource sqlSource) {
        MetaObject metaObject = SystemMetaObject.forObject(sqlSource);
        this.sql = (String) metaObject.getValue("sql");
        this.parameterMappings = (List<ParameterMapping>) metaObject.getValue("parameterMappings");
        this.configuration = (Configuration) metaObject.getValue("configuration");
        this.rawSqlSource = sqlSource;
    }

    /**
     * {@inheritDoc}
     *
     * @author 李远明
     * @version 2016年7月20日 下午8:29:02
     */
    @Override
    public BoundSql getBoundSql(Object parameterObject) {
        return new BoundSql(configuration, sql, parameterMappings, parameterObject);
    }

    public SqlSource getRawSqlSource() {
        return rawSqlSource;
    }

}
